#
#   EventHandler.py
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 of the License.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   Copyright
#   Author: Nicholas F. Hoover
#   Contributors: Salvatore S. Gionfriddo
#   Created: 2007.06.29
#   Last Modified: 2007.11.18
#

import pygame, sys
from Options import Options

        
def waitForKeyOrQuit(key, onQuitKeyFunction = None):
    event = pygame.event.wait()
    
    while True:
        if event.type == pygame.KEYDOWN and event.key == key:
            break
        elif event.type == pygame.KEYDOWN and event.key == Options.CONTROLS['QUIT']:
            if onQuitKeyFunction:
                onQuitKeyFunction()
                break
            else:
                sys.exit()
        elif event.type == pygame.QUIT:
            sys.exit()
        else:
            event = pygame.event.wait() 

class EventHandler(object):

    def __init__(self):
        None

    def doEvents(self, events):
        None

            
class GameEventHandler(EventHandler):
    
    def __init__(self, game):
        EventHandler.__init__(self)
        self.game = game
        self.gObjMan = self.game.gameObjectManager
        self.hero = self.gObjMan.hero
        
    def doEvents(self, events):
        for event in events:
            if event.type == pygame.QUIT:
                quit()
            elif event.type == pygame.KEYDOWN:

                if event.key == Options.CONTROLS['QUIT']:
                    self.game.pauseIt(doQuit = True)
                    
                #IN GAME MENU
                
                elif event.key == Options.CONTROLS['SUBMENU_TOGGLE']:
                    if self.game.hkms.on:
                        self.game.hkms.reset()
                    else:
                        self.game.hkms.on = True
                elif event.key == pygame.K_z and self.game.hkms.on:
                    self.game.hkms.undo()
                elif (event.key >= pygame.K_0 and event.key <= pygame.K_9) and self.game.hkms.on:
                    self.game.hkms.keyPress(event.key)
                    
                #END IN GAME MENU    
                    
                elif event.key == Options.CONTROLS['ADD_KID']:
                    self.gObjMan.addBasicKid()
                elif event.key == Options.CONTROLS['FPS_TOGGLE']:
                    self.game.hud.toggleFPS()
                    
                elif event.key == Options.CONTROLS['PAUSE']:
                    self.game.pause = not self.game.pause
                elif event.key == Options.CONTROLS['MUSIC_TOGGLE']:
                    self.game.toggleMusic()

                elif event.key == Options.CONTROLS['UP']:
                    self.hero.walk(self.hero.WALK_UP)
                elif event.key == Options.CONTROLS['DOWN']:
                    self.hero.walk(self.hero.WALK_DOWN)
                elif event.key == Options.CONTROLS['LEFT']:
                    self.hero.walk(self.hero.WALK_SIDE, self.hero.LEFT)
                elif event.key == Options.CONTROLS['RIGHT']:
                    self.hero.walk(self.hero.WALK_SIDE, self.hero.RIGHT)

                elif event.key == Options.CONTROLS['PUNCH']:
                    self.hero.attack1()
                    
                elif event.key == Options.CONTROLS['KICK']:
                    self.hero.attack2()
                    
                elif event.key == Options.CONTROLS['EAT']:
                    self.hero.eat()
                elif event.key == Options.CONTROLS['FLAME']:
                    self.hero.flame()
                elif event.key == Options.CONTROLS['GAS']:
                    self.hero.gasAttack()
                elif event.key ==Options.CONTROLS['PICKUP']:
                    self.hero.pickup()
                    
            elif event.type == pygame.KEYUP:
                if event.key == Options.CONTROLS['UP']:
                    self.hero.stopWalk(self.hero.WALK_UP)
                elif event.key == Options.CONTROLS['DOWN']:
                    self.hero.stopWalk(self.hero.WALK_DOWN)
                elif event.key == Options.CONTROLS['LEFT']:
                    self.hero.stopWalk(self.hero.WALK_SIDE, self.hero.LEFT)
                elif event.key == Options.CONTROLS['RIGHT']:
                    self.hero.stopWalk(self.hero.WALK_SIDE, self.hero.RIGHT)

